<?php
class frbMessage_MainController extends DooController 
{
  
  private $name = 'frb Message';
  private $col = 1;
  private $max_row = 10;
  
  /**
   * 
   * Retourne le widget du module
   * @return Array [name]=>[contenu] contenu du widget
   */
  public function getWidget() {
    Doo::loadModelAt('FrbMessage', 'frbMessage');
    $message = new FrbMessage();
    $message->mes_flag = 1;
    $message = $this->db()->find($message);
    $count = sizeof($message);
    echo '<h1>Widget du Module Message</h1>';
    echo '<p>'.$count.' nouveaux messages</p>';
    
  }
  
  public function getWidgetTitle() {
    echo "Message";
  }
  
  public function getMenu() {
    Doo::loadModelAt('FrbMessage', 'frbMessage');
    $message = new FrbMessage();
    $message->mes_flag = 1;
    $message = $this->db()->find($message);
    $count = sizeof($message);
    echo "Message<div class='new_message_menu'>$count</div>";
  }
  
  /**
   * 
   * Retourne les propriétés du widget Son nom et son nombre de colonne
   * @return Array [name]=>[nbColonne]
   */
  public function getWidgetProperties() {
    echo $this->col;
  }
  
  public function index() {
    Doo::loadModelAt('FrbMessage', 'frbMessage');
    $message = new FrbMessage();
    if ($this->isAjax()) {
      if ($this->isPost()) {
        if (!empty($_POST["action"]) && $_POST["action"] == "del" && !empty($_POST["id"]) && is_numeric($_POST["id"])) {
          $message->mes_id = $_POST['id'];
          $this->db()->delete($message);
          echo "Message supprimer";
          return;
        }
      }
      echo "Erreur lors de la suppression du message";
      return;
    }
    $data['baseurl'] = Doo::conf()->APP_URL;
    $data['total'] = ceil($message->count()/$this->max_row);
    if ($data['total'] == 0) $data['total'] = 1;
    $page = (!empty($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] >0 && $_GET['page']<=$data['total']) ? $_GET['page'] : 1;
    $rows = ($page-1) * $this->max_row;
    $data['mailInbox'] = $this->db()->find($message, array('desc'=>'mes_date', 'limit'=>"$rows, $this->max_row"));;
    $data['previous_page'] = $page-1;
    $data['next_page'] = ($page+1 >$data['total']) ? $data['total'] : $page+1;
    $data['page'] = $page;
    
    /*foreach ($data['mailInbox'] as $mail) {
      $mail->mes_mes = html_entity_decode($mail->mes_mes);
    }*/
    
    $this->render('inbox', $data);
  }
  
  public function readMessage() {
    if ($this->isAjax()) {
      if ($this->isPost()) {
        Doo::loadModelAt('FrbMessage', 'frbMessage');
        $message = new FrbMessage();
        $message->mes_id = $_POST['id'];
        $message = $this->db()->find($message, array('limit'=>1));
        //echo $message->mes_id;
        $message->mes_flag = 0;
        $this->db()->update($message);
      }
    }
  }
  
  public function countNewMessage() {
    Doo::loadModelAt('FrbMessage', 'frbMessage');
    $message = new FrbMessage();
    $message->mes_flag = 1;
    $message = $this->db()->find($message);
    $count = sizeof($message);
    echo $count;
  }
  
  private function isPost() {
    if ($_SERVER['REQUEST_METHOD'] == "POST") return true;
    else return false;
  }
  
}